
package packet.creators;

import client.DbgPet;
import client.DbgPlayer;
import client.stats.PlayerStat;
import packet.opcode.SendPacketOpcode;
import packet.transfer.write.Packet;
import packet.transfer.write.WritingDbg;
import server.movement.LifeMovementFragment;
import java.util.List;
import provider.DbgData;
import provider.DbgDataProviderFactory;

public class PetPacket {

    public static final Packet updatePet(final DbgPlayer player, final DbgPet pet, final boolean unequip, final boolean petLoot) {
	final WritingDbg mplew = new WritingDbg();
	mplew.writeShort(SendPacketOpcode.MODIFY_INVENTORY_ITEM.getValue());
	mplew.write(0);
	mplew.write(2);
        mplew.write(0);
	mplew.write(3);
	mplew.write(5);
	mplew.write(pet.getInventoryPosition());
	mplew.writeShort(0);
	mplew.write(5);
	mplew.write(pet.getInventoryPosition());
	mplew.write(0);
	mplew.write(3);
	mplew.writeInt(pet.getPetItemId());
        PacketProvider.addPetItemInfo(player, mplew, pet, unequip, petLoot);
        
	return mplew.getPacket();
    }

    public static final Packet showPet(final DbgPlayer chr, final DbgPet pet, final boolean remove, final boolean hunger) {

        final WritingDbg mplew = new WritingDbg();
        
	mplew.writeShort(SendPacketOpcode.SPAWN_PET.getValue());
	mplew.writeInt(chr.getId());
	mplew.writeInt(chr.getPetIndex(pet));

	if (remove) {
            mplew.writeShort(hunger ? 0x100 : 0);
	} else {
            mplew.write(1);
            mplew.write(hunger ? 1 : 0);
	    mplew.writeInt(pet.getPetItemId());
	    mplew.writeMapleAsciiString(pet.getName());
	    mplew.writeInt(pet.getUniqueId());
	    mplew.writeInt(0);
	    mplew.writeShort(pet.getPos().x);
	    mplew.writeShort(pet.getPos().y - 20);
	    mplew.write(pet.getStance());
	    mplew.writeShort(pet.getFh());
           //int item = new ArrayList("4033311", "4033302", "4033303", "4032002", "4033312", "4033304", "4000659");
            
            int color = pet.getPetItemId();         
            if (color == 5000244 ){
                color = -190;
            } else if (color == 5000243){
                color = -10;
            } else if (color == 5000230){
                color = -40;                
            } else if (color == 5000245){
                color = -20;
            } else if (color == 5000229){
                color = -230;
            } else if (color == 5000250){
                color = -10;
            } else if (color == 5000249){
                color = -60;//45
            } else if (color == 5000251){
                color = -220;//210
            } else if (color == 5000228){
                color = -1;
            }
            mplew.writeInt(color);            
	}
	return mplew.getPacket();
    }
    

    public static final Packet movePet(final int cid, final int pid, final byte slot, final List<LifeMovementFragment> moves) {
	final WritingDbg mplew = new WritingDbg();

	mplew.writeShort(SendPacketOpcode.MOVE_PET.getValue());
	mplew.writeInt(cid);
	mplew.writeInt(slot);
	mplew.writeLong(pid);
	PacketProvider.serializeMovementList(mplew, moves);

	return mplew.getPacket();
    }

    public static final Packet petChat(final int cid, final int un, final String text, final byte slot) {
	final WritingDbg mplew = new WritingDbg();

	mplew.writeShort(SendPacketOpcode.PET_CHAT.getValue());
	mplew.writeInt(cid);
	mplew.writeInt(slot);
	mplew.writeShort(un);
	mplew.writeMapleAsciiString(text);
	mplew.write(0);

	return mplew.getPacket();
    }

    public static final Packet commandResponse(final int cid, final byte command, final byte slot, final boolean success, final boolean food) {
	final WritingDbg mplew = new WritingDbg();

        //[D9 00] [82 57 72 00] 00 00 00 00 00 01 01
        //[D9 00] [82 57 72 00] 00 00 00 00 00 01 00
        //[D9 00] [82 57 72 00] 00 00 00 00 01 01

	mplew.writeShort(SendPacketOpcode.PET_COMMAND.getValue());
	mplew.writeInt(cid);
	mplew.writeInt(slot);
	mplew.write(command == 1 ? 1 : 0);
	mplew.write(command);
	if (command != 1) {
	    mplew.write (success ? 1 : 0);
	}
        mplew.writeInt(0); //v173
	return mplew.getPacket();
    }

    public static final Packet showOwnPetLevelUp(final byte index) {
	final WritingDbg mplew = new WritingDbg();

	mplew.writeShort(SendPacketOpcode.SHOW_ITEM_GAIN_INCHAT.getValue());
	mplew.write(6);
	mplew.write(0);
	mplew.writeInt(index); // Pet Index

	return mplew.getPacket();
    }

    public static final Packet showPetLevelUp(final DbgPlayer chr, final byte index) {
	final WritingDbg mplew = new WritingDbg();

	mplew.writeShort(SendPacketOpcode.SHOW_FOREIGN_EFFECT.getValue());
	mplew.writeInt(chr.getId());
	mplew.write(6);
	mplew.write(0);
	mplew.writeInt(index);

	return mplew.getPacket();
    }

    public static final Packet emptyStatUpdate() {
	final WritingDbg mplew = new WritingDbg();

	mplew.writeShort(SendPacketOpcode.UPDATE_STATS.getValue());
	mplew.write(1);
	mplew.writeLong(0);
	mplew.writeLong(0);

	return mplew.getPacket();
    }

    public static final Packet petStatUpdate(final DbgPlayer chr) {
	final WritingDbg mplew = new WritingDbg();

        mplew.writeShort(SendPacketOpcode.UPDATE_STATS.getValue());
        mplew.write(0);
        mplew.writeLong(PlayerStat.PET.getValue());
        
        

        byte count = 0;
        for (final DbgPet pet : chr.getPets()) {
            //if (pet.getSummoned()) {
                mplew.writeLong(pet.getUniqueId());
                count++;
            //}
        }
        while (count < 3) {
            mplew.write0(8);
            count++;
        }
        mplew.write(0);
	mplew.writeShort(0);

	return mplew.getPacket();
    }

    public static final Packet weirdStatUpdate(final DbgPet pet) {
	final WritingDbg mplew = new WritingDbg();

	mplew.writeShort(SendPacketOpcode.UPDATE_STATS.getValue());
	mplew.write(0);
	mplew.write(8);
	mplew.write(0);
	mplew.write(0x18);
	mplew.write(0);
	mplew.writeInt(pet.getUniqueId());
	mplew.writeLong(0);
	mplew.writeLong(0);
	mplew.writeInt(0);
	mplew.write(1);

	return mplew.getPacket();
    }
    
    public static final Packet updatePetLootStatus(int status) {
        final WritingDbg packet = new WritingDbg();
        
        packet.writeShort(SendPacketOpcode.PET_LOOT_STATUS.getValue());
        packet.write(status);
        packet.write(1);
        
        return packet.getPacket();
    }    
}
